package com.lxy.dao;

import com.lxy.entity.DoctorAdvice;
import com.lxy.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DoctorAdviceDao {

    public List<DoctorAdvice> queryDoctorAdviceList() {
        List<DoctorAdvice> doctorAdviceList = new ArrayList<>();
        Connection connection = null;
        try {
            connection = DBUtils.getConnection();
            String query = "select * from doctoradvice";
            PreparedStatement preparedStatement = connection.prepareStatement(query);
            ResultSet set = preparedStatement.executeQuery();
            while (set.next()) {
                DoctorAdvice doctorAdvice = new DoctorAdvice();
                doctorAdvice.setDaid(set.getInt("daid"));
                doctorAdvice.setRoid(set.getInt("roid"));
                doctorAdvice.setMemeberid(set.getInt("memberid"));
                doctorAdvice.setDocid(set.getInt("docid"));
                doctorAdvice.setResults(set.getString("results"));
                doctorAdvice.setPrescription(set.getString("prescription"));
                doctorAdvice.setCreatedate(set.getDate("createdate").toString());
                doctorAdvice.setStatus(set.getInt("status"));
                doctorAdviceList.add(doctorAdvice);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doctorAdviceList;
    }

    public DoctorAdvice queryDoctorAdvice(Integer roid) {
        DoctorAdvice doctorAdvice = null;
        String query = "SELECT * FROM doctoradvice WHERE roid=?";
        try (Connection connection = DBUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(query)) {
            ps.setInt(1, roid);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    doctorAdvice = new DoctorAdvice();
                    doctorAdvice.setDaid(rs.getInt("daid"));
                    doctorAdvice.setRoid(rs.getInt("roid"));
                    doctorAdvice.setMemeberid(rs.getInt("memberid"));
                    doctorAdvice.setDocid(rs.getInt("docid"));
                    doctorAdvice.setResults(rs.getString("results"));
                    doctorAdvice.setPrescription(rs.getString("prescription"));
                    doctorAdvice.setCreatedate(rs.getDate("createdate").toString());
                    doctorAdvice.setStatus(rs.getInt("status"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return doctorAdvice;
    }

    public Integer insertDoctorAdvice(DoctorAdvice doctorAdvice) {
        Integer affectedRows = 0;
        try (Connection connection = DBUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(
                     "INSERT INTO doctoradvice(daid, roid, memberid, docid, results, prescription, createdate, status) VALUES (?,?,?,?,?,?,?,?)")) {
            ps.setInt(1, doctorAdvice.getDaid());
            ps.setInt(2, doctorAdvice.getRoid());
            ps.setInt(3, doctorAdvice.getMemeberid());
            ps.setInt(4, doctorAdvice.getDocid());
            ps.setString(5, doctorAdvice.getResults());
            ps.setString(6, doctorAdvice.getPrescription());
            ps.setString(7, doctorAdvice.getCreatedate());
            ps.setInt(8, doctorAdvice.getStatus());
            affectedRows = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return affectedRows;
    }

    public Integer kaiYao(Integer roid, String prescription) throws SQLException {
        Integer affectedRows = 0;
        try (Connection connection = DBUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(
                     "UPDATE doctoradvice SET prescription = concat(prescription, ?) WHERE roid=?")) {
            ps.setString(1, prescription);
            ps.setInt(2, roid);
            affectedRows = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return affectedRows;
    }
}